﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Web.Models;
using KL.Store.Core;
using KL.Store.Core.Domain;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using KL.Store.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using System.Globalization;
using System.Threading;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.StorageClient.Protocol;
using Microsoft.WindowsAzure;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Net;

namespace KL.Store.Web.Controllers
{
    public class CategoryManageController : KL.Store.Core.Web.BaseController
    {
        //
        // GET: /CategoryManage/

        public ActionResult Index()
        {
            return View();
        }

        #region Main

        public ActionResult Edit(int id, string toEdit = "")
        {
            if (!eStoreUser.IsManager && !eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (string.IsNullOrEmpty(toEdit))
            {
                toEdit = "EditMeta";
                if (!eStoreUser.IsManager)
                {
                    toEdit = "EditMeta";
                }
            }
            if (toEdit != "AddNewCategory")
            {
                CategoryMeta cm = dbCategory.GetMeta(id);
                ViewBag.CategoryName = cm.CategoryName;
            }
            ViewBag.id = id;
            ViewBag.ToEdit = toEdit;
            return View();
        }

        #endregion

        #region EditMenu

        public ActionResult EditMenu(int id, string toEdit)
        {
            ViewBag.IsManager = eStoreUser.IsManager;
            ViewBag.IsSeoManager = eStoreUser.IsSeoManager;
            ViewBag.CategoryId = id;
            ViewBag.toEdit = toEdit;
            return View();
        }

        #endregion

        #region EditMeta

        public ActionResult EditMeta(int id)
        {
            if (!eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.CategoryId = id;
            return View();
        }

        public ActionResult EditMetaGet(int id)
        {
            if (!eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            CategoryMetaEditModel categoryMetaEdit = new CategoryMetaEditModel();
            CategoryMeta cm = new CategoryMeta();
            cm = dbCategory.GetMeta(id);
            Mapper.MapDomainToViewModel2(cm, categoryMetaEdit);
            ViewBag.completeResult = "0";
            return View(categoryMetaEdit);
        }

        [HttpPost]
        public ActionResult EditMetaGet(CategoryMeta cm, string completeResult = "0")
        {
            if (!eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            cm.WebTitle = !string.IsNullOrEmpty(cm.WebTitle) ? cm.WebTitle : "";
            cm.Description = !string.IsNullOrEmpty(cm.Description) ? cm.Description : "";
            cm.Keywords = !string.IsNullOrEmpty(cm.Keywords) ? cm.Keywords : "";
            cm.Canonical = !string.IsNullOrEmpty(cm.Canonical) ? cm.Canonical : "";
            if (!string.IsNullOrEmpty(completeResult) && completeResult == "1")
            {
                ViewBag.completeResult = "1";
            }
            else
            {
                ViewBag.completeResult = "0";
            }
            int result = dbCategory.SaveCategoryMeta(cm);
            if (result == 1)
            {
                return JavaScript("urunKaydedildi('" + completeResult + "');");
            }
            else
            {
                return JavaScript("hataMesaji('Kayıt işlemi sırasında bir hata oluştu.');");
            }
        }


        #endregion

    }
}
